package cn.flyboy.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import cn.flyboy.entity.*;
import cn.flyboy.mapper.ExamMapper;
import cn.flyboy.mapper.StudentMapper;
import cn.flyboy.mapper.TeacherMapper;
import cn.flyboy.service.TeacherService;
import cn.flyboy.utils.FileHelper;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
public class TeacherServiceImpl implements TeacherService {
    @Resource
    private TeacherMapper teacherMapper;

    @Resource
    private ExamMapper examMapper;

    @Resource
    private StudentMapper studentMapper;

    private String originalFileName;
    private String path;
    private File targetFilePath;


    @Override
    public Teacher login(Teacher teacher) {
        return teacherMapper.selByTeacher(teacher);
    }

    @Override
    public List<Answer> selFileAnswerName(String examName, String teaName) {
        return examMapper.selFileAnswerName(examName, teaName);
    }

    @Override
    public List<the_answer> selStuAnswer(String examName, String teaName) {
        return examMapper.selStuAnswer(examName, teaName);
    }

    // 批量导入学生信息
    @Override
    public int saveTeaExcel(MultipartFile multipartFile, HttpServletRequest req) {
        // 获取文件名
        originalFileName = multipartFile.getOriginalFilename();
        System.out.println(originalFileName + "****************************************");

        // 上传路径保存设置
        path = req.getServletContext().getRealPath("/WEB-INF/files/");

        // (String parent,String Child)
        targetFilePath = new File(path, originalFileName);

        if (!targetFilePath.exists()) {
            targetFilePath.getParentFile().mkdirs();
            try {
                if (targetFilePath.createNewFile()) {
                    System.out.println(targetFilePath);
                    multipartFile.transferTo(targetFilePath);
                    if (importExcelToSQL(path + File.separator + originalFileName)) {
                        return 0;
                    } else {
                        return 3;
                    }
                }
            } catch (IOException e) {
                return 2;
            }
        }
        return 1;
    }

    public boolean importExcelToSQL(String fileUrl) {
        try (FileInputStream inputStream = new FileInputStream(fileUrl)) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            for (Row row : sheet) {
                if (row.getRowNum() == 0) {
                    continue;
                }

                Teacher teacher = new Teacher();

                row.getCell(0).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(1).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(2).setCellType(Cell.CELL_TYPE_STRING);
                row.getCell(3).setCellType(Cell.CELL_TYPE_NUMERIC);

                teacher.setT_username(row.getCell(0).getStringCellValue());
                teacher.setT_pwd(row.getCell(1).getStringCellValue());
                teacher.setT_name(row.getCell(2).getStringCellValue());
                teacher.setT_manager((int) row.getCell(3).getNumericCellValue());
                teacherMapper.insTeacher(teacher);
            }
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public Teacher selTeacher(String userID) {
        return teacherMapper.selTeacher(userID);
    }

    @Override
    public int selTeaNumOfId(String userID) {
        return teacherMapper.selTeaNumOfId(userID);
    }

    @Override
    public int getManagerCount() {
        return teacherMapper.selByManager().size();
    }

    @Override
    public List<Teacher> getAllTeacher() {
        return (List<Teacher>) teacherMapper.selAllTeacher();
    }

    @Override
    public void addTeacher(Teacher teacher) {
        teacherMapper.insTeacher(teacher);
    }

    @Override
    public int updPwd(Teacher teacher) {
        return teacherMapper.updPwd(teacher);
    }

    @Override
    public int delTeacher(String name) {
        return teacherMapper.delTeacher(name);
    }

    @Override
    public int updTeacher(Teacher teacher) {
        return teacherMapper.updTeacher(teacher);
    }

    @Override
    public int delAnswer(String examName, String teaName) {
        return examMapper.delAnswer(examName, teaName);
    }

    @Override
    public int delStudentOfExam(String exam) {
        return studentMapper.delStudentOfExam(exam);
    }

    @Override
    public int delStudentExam(String stuName, String stuExam) {
        return studentMapper.delStudentExam(stuName, stuExam);
    }

    @Override
    public int delExam(String examName, String teaName) {
        return examMapper.delExam(examName, teaName);
    }

    @Override
    public int deleteTeacherBatch(Set<String> ids) {
        return teacherMapper.deleteTeacherBatch(ids);
    }
}
